package client;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;


public class Setting extends JPanel implements ActionListener{
    static JTextField server,port;
     public static void createF() throws IOException{
            FileWriter fw;
            fw = new FileWriter("FileConnect.txt");
            fw.write(server.getText()+"\n"+port.getText());
            fw.flush();
            fw.close();
    }
    public Setting(){
        JLabel lserver = new JLabel("Server :");
        server = new JTextField(20);
        JPanel g1 = new JPanel();
        g1.setLayout(new BoxLayout(g1,BoxLayout.X_AXIS));
        lserver.setPreferredSize(new Dimension(100, 25));
        g1.add(lserver);
        g1.add(server);
        
        JLabel lport = new JLabel("Port :");
        port = new JTextField(20);
        JPanel g2 = new JPanel();
        g2.setLayout(new BoxLayout(g2,BoxLayout.X_AXIS));
        lserver.setPreferredSize(new Dimension(50, 25));
        g2.add(lport);
        g2.add(port);

        JPanel info = new JPanel();
        info.setLayout(new BoxLayout(info,BoxLayout.Y_AXIS));
        info.add(g1);
        info.add(g2);
        
        JButton b1 = new JButton("Change");
        b1.setActionCommand("CHANGE");
        b1.addActionListener(this);
        JPanel Button = new JPanel(new FlowLayout(FlowLayout.CENTER,5,5));
        Button.add(b1);

        setLayout(new BorderLayout(5,5));
        add(info,BorderLayout.PAGE_START);
        add(Button,BorderLayout.PAGE_END);
    }
    public void actionPerformed(ActionEvent e) {
        String strActionCommand = e.getActionCommand();
        if (strActionCommand.equals("CHANGE")){
            try {
                    JOptionPane.showMessageDialog(this, "Setting is successful");
                    createF();
                    Main.frameSetting.dispose();
                    try {
                        Main.main(null);
                    } catch (ClassNotFoundException ex) {
                        JOptionPane.showMessageDialog(this, ex.getMessage());
                    }

            }
            catch (IOException ex) {
                JOptionPane.showMessageDialog(this, ex.getMessage());
            }
        }
    }

}